<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="按键和液晶，可以组成我们最简易的计算器。下面我们来写一个简易整数计算器提供给大家学习。为了让程序不过于复杂，我们这个计算器不考虑连加，连减等连续计算，不考虑小数情况。加减乘除分别用上下左右来替代，回车表示等于，ESC 表示归 0。程序共分为三部分，一部分是 1602 液晶显示，一部分是按键动作和扫描，一部分是主函数功能。 12345678910111213141516171819202122232">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="13.4、单片机计算器实例">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2014/12/01/mcu088/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="按键和液晶，可以组成我们最简易的计算器。下面我们来写一个简易整数计算器提供给大家学习。为了让程序不过于复杂，我们这个计算器不考虑连加，连减等连续计算，不考虑小数情况。加减乘除分别用上下左右来替代，回车表示等于，ESC 表示归 0。程序共分为三部分，一部分是 1602 液晶显示，一部分是按键动作和扫描，一部分是主函数功能。 12345678910111213141516171819202122232">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-28T12:57:06.018Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="13.4、单片机计算器实例">
<meta name="twitter:description" content="按键和液晶，可以组成我们最简易的计算器。下面我们来写一个简易整数计算器提供给大家学习。为了让程序不过于复杂，我们这个计算器不考虑连加，连减等连续计算，不考虑小数情况。加减乘除分别用上下左右来替代，回车表示等于，ESC 表示归 0。程序共分为三部分，一部分是 1602 液晶显示，一部分是按键动作和扫描，一部分是主函数功能。 12345678910111213141516171819202122232">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2014/12/01/mcu088/">





  <title>13.4、单片机计算器实例 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2014/12/01/mcu088/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">13.4、单片机计算器实例</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2014-12-01T20:37:11+08:00">
                2014-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/单片机教程/" itemprop="url" rel="index">
                    <span itemprop="name">单片机教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>按键和液晶，可以组成我们最简易的计算器。下面我们来写一个简易整数计算器提供给大家学习。为了让程序不过于复杂，我们这个计算器不考虑连加，连减等连续计算，不考虑小数情况。加减乘除分别用上下左右来替代，回车表示等于，ESC 表示归 0。程序共分为三部分，一部分是 1602 液晶显示，一部分是按键动作和扫描，一部分是主函数功能。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">/***************************Lcd1602.c 文件程序源代码*****************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">#define LCD1602_DB P0</span><br><span class="line">sbit LCD1602_RS = P1^0;</span><br><span class="line">sbit LCD1602_RW = P1^1;</span><br><span class="line">sbit LCD1602_E = P1^5;</span><br><span class="line">/* 等待液晶准备好 */</span><br><span class="line">void LcdWaitReady()&#123;</span><br><span class="line">    unsigned char sta;</span><br><span class="line">    LCD1602_DB = 0xFF;</span><br><span class="line">    LCD1602_RS = 0;</span><br><span class="line">    LCD1602_RW = 1;</span><br><span class="line">    do &#123;</span><br><span class="line">        LCD1602_E = 1;</span><br><span class="line">        sta = LCD1602_DB; //读取状态字</span><br><span class="line">        LCD1602_E = 0;</span><br><span class="line">    //bit7 等于 1 表示液晶正忙，重复检测直到其等于 0 为止</span><br><span class="line">    &#125;while (sta &amp; 0x80);</span><br><span class="line">&#125;</span><br><span class="line">/* 向 LCD1602 液晶写入一字节命令，cmd-待写入命令值 */</span><br><span class="line">void LcdWriteCmd(unsigned char cmd)&#123;</span><br><span class="line">    LcdWaitReady();</span><br><span class="line">    LCD1602_RS = 0;</span><br><span class="line">    LCD1602_RW = 0;</span><br><span class="line">    LCD1602_DB = cmd;</span><br><span class="line">    LCD1602_E = 1;</span><br><span class="line">    LCD1602_E = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 向 LCD1602 液晶写入一字节数据，dat-待写入数据值 */</span><br><span class="line">void LcdWriteDat(unsigned char dat)&#123;</span><br><span class="line">    LcdWaitReady();</span><br><span class="line">    LCD1602_RS = 1;</span><br><span class="line">    LCD1602_RW = 0;</span><br><span class="line">    LCD1602_DB = dat;</span><br><span class="line">    LCD1602_E = 1;</span><br><span class="line">    LCD1602_E = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 设置显示 RAM 起始地址，亦即光标位置，(x,y)-对应屏幕上的字符坐标 */</span><br><span class="line">void LcdSetCursor(unsigned char x, unsigned char y)&#123;</span><br><span class="line">    unsigned char addr;</span><br><span class="line">    if (y == 0)&#123; //由输入的屏幕坐标计算显示 RAM 的地址</span><br><span class="line">        addr = 0x00 + x; //第一行字符地址从 0x00 起始</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        addr = 0x40 + x; //第二行字符地址从 0x40 起始</span><br><span class="line">    &#125;</span><br><span class="line">    LcdWriteCmd(addr | 0x80); //设置 RAM 地址</span><br><span class="line">&#125;</span><br><span class="line">/* 在液晶上显示字符串，(x,y)-对应屏幕上的起始坐标，str-字符串指针 */</span><br><span class="line">void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)&#123;</span><br><span class="line">    LcdSetCursor(x, y); //设置起始地址</span><br><span class="line">    while (*str != &apos;\0&apos;)&#123; //连续写入字符串数据，直到检测到结束符</span><br><span class="line">        LcdWriteDat(*str++);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 区域清除，清除从(x,y)坐标起始的 len 个字符位 */</span><br><span class="line">void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len)&#123;</span><br><span class="line">    LcdSetCursor(x, y); //设置起始地址</span><br><span class="line">    while (len--)&#123; //连续写入空格</span><br><span class="line">        LcdWriteDat(&apos; &apos;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 整屏清除 */</span><br><span class="line">void LcdFullClear()&#123;</span><br><span class="line">    LcdWriteCmd(0x01);</span><br><span class="line">&#125;</span><br><span class="line">/* 初始化 1602 液晶 */</span><br><span class="line">void InitLcd1602()&#123;</span><br><span class="line">    LcdWriteCmd(0x38); //16*2 显示，5*7 点阵，8 位数据接口</span><br><span class="line">    LcdWriteCmd(0x0C); //显示器开，光标关闭</span><br><span class="line">    LcdWriteCmd(0x06); //文字不动，地址自动+1</span><br><span class="line">    LcdWriteCmd(0x01); //清屏</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>Lcd1602.c 文件中根据上层应用的需要增加了 2 个清屏函数：区域清屏——LcdAreaClear，整屏清屏——LcdFullClear。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line">/**************************keyboard.c 文件程序源代码*****************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">sbit KEY_IN_1 = P2^4;</span><br><span class="line">sbit KEY_IN_2 = P2^5;</span><br><span class="line">sbit KEY_IN_3 = P2^6;</span><br><span class="line">sbit KEY_IN_4 = P2^7;</span><br><span class="line">sbit KEY_OUT_1 = P2^3;</span><br><span class="line">sbit KEY_OUT_2 = P2^2;</span><br><span class="line">sbit KEY_OUT_3 = P2^1;</span><br><span class="line">sbit KEY_OUT_4 = P2^0;</span><br><span class="line">unsigned char code KeyCodeMap[4][4] = &#123; //矩阵按键编号到标准键盘键码的映射表</span><br><span class="line">    &#123; &apos;1&apos;, &apos;2&apos;, &apos;3&apos;, 0x26 &#125;, //数字键 1、数字键 2、数字键 3、向上键</span><br><span class="line">    &#123; &apos;4&apos;, &apos;5&apos;, &apos;6&apos;, 0x25 &#125;, //数字键 4、数字键 5、数字键 6、向左键</span><br><span class="line">    &#123; &apos;7&apos;, &apos;8&apos;, &apos;9&apos;, 0x28 &#125;, //数字键 7、数字键 8、数字键 9、向下键</span><br><span class="line">    &#123; &apos;0&apos;, 0x1B, 0x0D, 0x27 &#125; //数字键 0、ESC 键、 回车键、 向右键</span><br><span class="line">&#125;;</span><br><span class="line">unsigned char pdata KeySta[4][4] = &#123; //全部矩阵按键的当前状态</span><br><span class="line">    &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;</span><br><span class="line">&#125;;</span><br><span class="line">extern void KeyAction(unsigned char keycode);</span><br><span class="line">/* 按键驱动函数，检测按键动作，调度相应动作函数，需在主循环中调用 */</span><br><span class="line">void KeyDriver()&#123;</span><br><span class="line">    unsigned char i, j;</span><br><span class="line">    static unsigned char pdata backup[4][4] = &#123; //按键值备份，保存前一次的值</span><br><span class="line">        &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;, &#123;1, 1, 1, 1&#125;</span><br><span class="line">    &#125;;</span><br><span class="line">   </span><br><span class="line">    for (i=0; i&lt;4; i++)&#123; //循环检测 4*4 的矩阵按键</span><br><span class="line">        for (j=0; j&lt;4; j++)&#123;</span><br><span class="line">            if (backup[i][j] != KeySta[i][j])&#123; //检测按键动作</span><br><span class="line">                if (backup[i][j] != 0)&#123; //按键按下时执行动作</span><br><span class="line">                    KeyAction(KeyCodeMap[i][j]); //调用按键动作函数</span><br><span class="line">                &#125;</span><br><span class="line">                backup[i][j] = KeySta[i][j]; //刷新前一次的备份值</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 按键扫描函数，需在定时中断中调用，推荐调用间隔 1ms */</span><br><span class="line">void KeyScan()&#123;</span><br><span class="line">    unsigned char i;</span><br><span class="line">    static unsigned char keyout = 0; //矩阵按键扫描输出索引</span><br><span class="line">    static unsigned char keybuf[4][4] = &#123; //矩阵按键扫描缓冲区</span><br><span class="line">        &#123;0xFF, 0xFF, 0xFF, 0xFF&#125;, &#123;0xFF, 0xFF, 0xFF, 0xFF&#125;,</span><br><span class="line">        &#123;0xFF, 0xFF, 0xFF, 0xFF&#125;, &#123;0xFF, 0xFF, 0xFF, 0xFF&#125;</span><br><span class="line">    &#125;;</span><br><span class="line">   </span><br><span class="line">    //将一行的 4 个按键值移入缓冲区</span><br><span class="line">    keybuf[keyout][0] = (keybuf[keyout][0] &lt;&lt; 1) | KEY_IN_1;</span><br><span class="line">    keybuf[keyout][1] = (keybuf[keyout][1] &lt;&lt; 1) | KEY_IN_2;</span><br><span class="line">    keybuf[keyout][2] = (keybuf[keyout][2] &lt;&lt; 1) | KEY_IN_3;</span><br><span class="line">    keybuf[keyout][3] = (keybuf[keyout][3] &lt;&lt; 1) | KEY_IN_4;</span><br><span class="line">    //消抖后更新按键状态</span><br><span class="line">    for (i=0; i&lt;4; i++)&#123; //每行 4 个按键，所以循环 4 次</span><br><span class="line">        if ((keybuf[keyout][i] &amp; 0x0F) == 0x00)&#123;</span><br><span class="line">            //连续 4 次扫描值为 0，即 4*4ms 内都是按下状态时，可认为按键已稳定的按下</span><br><span class="line">            KeySta[keyout][i] = 0;</span><br><span class="line">        &#125;else if ((keybuf[keyout][i] &amp; 0x0F) == 0x0F)&#123;</span><br><span class="line">            //连续 4 次扫描值为 1，即 4*4ms 内都是弹起状态时，可认为按键已稳定的弹起</span><br><span class="line">            KeySta[keyout][i] = 1;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    //执行下一次的扫描输出</span><br><span class="line">    keyout++; //输出索引递增</span><br><span class="line">    keyout &amp;= 0x03; //索引值加到 4 即归零</span><br><span class="line">    switch (keyout)&#123; //根据索引，释放当前输出引脚，拉低下次的输出引脚</span><br><span class="line">        case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;</span><br><span class="line">        case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;</span><br><span class="line">        case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;</span><br><span class="line">        case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;</span><br><span class="line">        default: break;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>keyboard.c 是对之前已经用过多次的矩阵按键驱动的封装，具体到某个按键要执行的动作函数都放到上层的 main.c 中实现，在这个按键驱动文件中只负责调用上层实现的按键动作函数即可。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br></pre></td><td class="code"><pre><span class="line">/*****************************main.c 文件程序源代码******************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">unsigned char step = 0; //操作步骤</span><br><span class="line">unsigned char oprt = 0; //运算类型</span><br><span class="line">signed long num1 = 0; //操作数 1</span><br><span class="line">signed long num2 = 0; //操作数 2</span><br><span class="line">signed long result = 0; //运算结果</span><br><span class="line">unsigned char T0RH = 0; //T0 重载值的高字节</span><br><span class="line">unsigned char T0RL = 0; //T0 重载值的低字节</span><br><span class="line">void ConfigTimer0(unsigned int ms);</span><br><span class="line">extern void KeyScan();</span><br><span class="line">extern void KeyDriver();</span><br><span class="line">extern void InitLcd1602();</span><br><span class="line">extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);</span><br><span class="line">extern void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len);</span><br><span class="line">extern void LcdFullClear();</span><br><span class="line">void main()&#123;</span><br><span class="line">    EA = 1; //开总中断</span><br><span class="line">    ConfigTimer0(1); //配置 T0 定时 1ms</span><br><span class="line">    InitLcd1602(); //初始化液晶</span><br><span class="line">    LcdShowStr(15, 1, &quot;0&quot;); //初始显示一个数字 0</span><br><span class="line">    while (1)&#123;</span><br><span class="line">        KeyDriver(); //调用按键驱动</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 长整型数转换为字符串，str-字符串指针，dat-待转换数，返回值-字符串长度 */</span><br><span class="line">unsigned char LongToString(unsigned char *str, signed long dat)&#123;</span><br><span class="line">    signed char i = 0;</span><br><span class="line">    unsigned char len = 0;</span><br><span class="line">    unsigned char buf[12];</span><br><span class="line">   </span><br><span class="line">    if (dat &lt; 0)&#123; //如果为负数，首先取绝对值，并在指针上添加负号</span><br><span class="line">        dat = -dat;</span><br><span class="line">        *str++ = &apos;-&apos;;</span><br><span class="line">        len++;</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br><span class="line">    do &#123; //先转换为低位在前的十进制数组</span><br><span class="line">        buf[i++] = dat % 10;</span><br><span class="line">        dat /= 10;</span><br><span class="line">    &#125; while (dat &gt; 0);</span><br><span class="line">    len += i; //i 最后的值就是有效字符的个数</span><br><span class="line">    while (i-- &gt; 0)&#123; //将数组值转换为 ASCII 码反向拷贝到接收指针上</span><br><span class="line">        *str++ = buf[i] + &apos;0&apos;;</span><br><span class="line">    &#125;</span><br><span class="line">    *str = &apos;\0&apos;; //添加字符串结束符</span><br><span class="line">    return len; //返回字符串长度</span><br><span class="line">&#125;</span><br><span class="line">/* 显示运算符，显示位置 y，运算符类型 type */</span><br><span class="line">void ShowOprt(unsigned char y, unsigned char type)&#123;</span><br><span class="line">    switch (type)&#123;</span><br><span class="line">        case 0: LcdShowStr(0, y, &quot;+&quot;); break; //0 代表+</span><br><span class="line">        case 1: LcdShowStr(0, y, &quot;-&quot;); break; //1 代表-</span><br><span class="line">        case 2: LcdShowStr(0, y, &quot;*&quot;); break; //2 代表*</span><br><span class="line">        case 3: LcdShowStr(0, y, &quot;/&quot;); break; //3 代表/</span><br><span class="line">        default: break;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 计算器复位，清零变量值，清除屏幕显示 */</span><br><span class="line">void Reset()&#123;</span><br><span class="line">    num1 = 0;</span><br><span class="line">    num2 = 0;</span><br><span class="line">    step = 0;</span><br><span class="line">    LcdFullClear();</span><br><span class="line">&#125;</span><br><span class="line">/* 数字键动作函数，n-按键输入的数值 */</span><br><span class="line">void NumKeyAction(unsigned char n)&#123;</span><br><span class="line">    unsigned char len;</span><br><span class="line">    unsigned char str[12];</span><br><span class="line">   </span><br><span class="line">    if (step &gt; 1)&#123; //如计算已完成，则重新开始新的计算</span><br><span class="line">        Reset();</span><br><span class="line">    &#125;</span><br><span class="line">    if (step == 0)&#123; //输入第一操作数</span><br><span class="line">        num1 = num1*10 + n; //输入数值累加到原操作数上</span><br><span class="line">        len = LongToString(str, num1); //新数值转换为字符串</span><br><span class="line">        LcdShowStr(16-len, 1, str); //显示到液晶第二行上</span><br><span class="line">    &#125;else&#123; //输入第二操作数</span><br><span class="line">        num2 = num2*10 + n; //输入数值累加到原操作数上</span><br><span class="line">        len = LongToString(str, num2); //新数值转换为字符串</span><br><span class="line">        LcdShowStr(16-len, 1, str); //显示到液晶第二行上</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 运算符按键动作函数，运算符类型 type */</span><br><span class="line">void OprtKeyAction(unsigned char type)&#123;</span><br><span class="line">    unsigned char len;</span><br><span class="line">    unsigned char str[12];</span><br><span class="line">   </span><br><span class="line">    if (step == 0)&#123; //第二操作数尚未输入时响应，即不支持连续操作</span><br><span class="line">        len = LongToString(str, num1); //第一操作数转换为字符串</span><br><span class="line">        LcdAreaClear(0, 0, 16-len); //清除第一行左边的字符位</span><br><span class="line">        LcdShowStr(16-len, 0, str); //字符串靠右显示在第一行</span><br><span class="line">        ShowOprt(1, type); //在第二行显示操作符</span><br><span class="line">        LcdAreaClear(1, 1, 14); //清除第二行中间的字符位</span><br><span class="line">        LcdShowStr(15, 1, &quot;0&quot;); //在第二行最右端显示 0</span><br><span class="line">        oprt = type; //记录操作类型</span><br><span class="line">        step = 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 计算结果函数 */</span><br><span class="line">void GetResult()&#123;</span><br><span class="line">    unsigned char len;</span><br><span class="line">    unsigned char str[12];</span><br><span class="line">   </span><br><span class="line">    if (step == 1)&#123; //第二操作数已输入时才执行计算</span><br><span class="line">        step = 2;</span><br><span class="line">        switch (oprt)&#123; //根据运算符类型计算结果，未考虑溢出问题</span><br><span class="line">            case 0: result = num1 + num2; break;</span><br><span class="line">            case 1: result = num1 - num2; break;</span><br><span class="line">            case 2: result = num1 * num2; break;</span><br><span class="line">            case 3: result = num1 / num2; break;</span><br><span class="line">            default: break;</span><br><span class="line">        &#125;</span><br><span class="line">        len = LongToString(str, num2); //原第二操作数和运算符显示到第一行</span><br><span class="line">        ShowOprt(0, oprt);</span><br><span class="line">        LcdAreaClear(1, 0, 16-1-len);</span><br><span class="line">        LcdShowStr(16-len, 0, str);</span><br><span class="line">        len = LongToString(str, result); //计算结果和等号显示在第二行</span><br><span class="line">        LcdShowStr(0, 1, &quot;=&quot;);</span><br><span class="line">        LcdAreaClear(1, 1, 16-1-len);</span><br><span class="line">        LcdShowStr(16-len, 1, str);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 按键动作函数，根据键码执行相应的操作，keycode-按键键码 */</span><br><span class="line">void KeyAction(unsigned char keycode)&#123;</span><br><span class="line">    if ((keycode&gt;=&apos;0&apos;) &amp;&amp; (keycode&lt;=&apos;9&apos;))&#123; //输入字符</span><br><span class="line">        NumKeyAction(keycode - &apos;0&apos;);</span><br><span class="line">    &#125;else if (keycode == 0x26)&#123; //向上键，+</span><br><span class="line">        OprtKeyAction(0);</span><br><span class="line">    &#125;else if (keycode == 0x28)&#123; //向下键，-</span><br><span class="line">        OprtKeyAction(1);</span><br><span class="line">    &#125;else if (keycode == 0x25)&#123; //向左键，*</span><br><span class="line">        OprtKeyAction(2);</span><br><span class="line">    &#125;else if (keycode == 0x27)&#123; //向右键，÷</span><br><span class="line">        OprtKeyAction(3);</span><br><span class="line">    &#125;else if (keycode == 0x0D)&#123; //回车键，计算结果</span><br><span class="line">        GetResult();</span><br><span class="line">    &#125;else if (keycode == 0x1B)&#123; //Esc 键，清除</span><br><span class="line">        Reset();</span><br><span class="line">        LcdShowStr(15, 1, &quot;0&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 配置并启动 T0，ms-T0 定时时间 */</span><br><span class="line">void ConfigTimer0(unsigned int ms)&#123;</span><br><span class="line">    unsigned long tmp; //临时变量</span><br><span class="line">    tmp = 11059200 / 12; //定时器计数频率</span><br><span class="line">    tmp = (tmp * ms) / 1000; //计算所需的计数值</span><br><span class="line">    tmp = 65536 - tmp; //计算定时器重载值</span><br><span class="line">    tmp = tmp + 28; //补偿中断响应延时造成的误差</span><br><span class="line">    T0RH = (unsigned char)(tmp&gt;&gt;8); //定时器重载值拆分为高低字节</span><br><span class="line">    T0RL = (unsigned char)tmp;</span><br><span class="line">   </span><br><span class="line">    TMOD &amp;= 0xF0; //清零 T0 的控制位</span><br><span class="line">    TMOD |= 0x01; //配置 T0 为模式 1</span><br><span class="line">    TH0 = T0RH; //加载 T0 重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    ET0 = 1; //使能 T0 中断</span><br><span class="line">    TR0 = 1; //启动 T0</span><br><span class="line">&#125;</span><br><span class="line">/* T0 中断服务函数，执行按键扫描 */</span><br><span class="line">void InterruptTimer0() interrupt 1&#123;</span><br><span class="line">    TH0 = T0RH; //重新加载重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    KeyScan(); //按键扫描</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>main.c 文件实现所有应用层的操作函数，即计算器功能所需要信息显示、按键动作响应等，另外还包括主循环和定时中断的调度。</p>
<p>通过这样一个程序，大家一方面学习如何进行多个.c 文件的编程，另外一个方面学会多个函数之间的灵活调用。可以把这个程序看成是一个简单的小项目，学习一下项目编程都是如何进行和布局的。不要把项目想象的太难，再复杂的项目也是这种简单程序的组合和扩展而已。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2014/12/01/mcu087/" rel="next" title="13.3、多个.c文件的初步认识">
                <i class="fa fa-chevron-left"></i> 13.3、多个.c文件的初步认识
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2014/12/01/mcu089/" rel="prev" title="13.5、串口通信原理和控制程序">
                13.5、串口通信原理和控制程序 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
